package com.vastcom.tyauthcenter.action;

import com.vastcom.tyauthcenter.model.*;
import com.vastcom.tyauthcenter.service.ManageService;
import com.vastcom.tyauthcenter.to.RoleResourceParm;
import com.vastcom.tyauthcenter.tools.Tools;
import com.vastcom.tyauthcenter.vo.MenuTreeVo;
import com.vastcom.tyauthcenter.vo.NzSelOption;
import com.vastcom.tyauthcenter.vo.NzTreeVo;
import com.vastcom.tyauthcenter.vo.PageParmVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@CrossOrigin
@RestController
@PreAuthorize("hasAuthority('member')")
@RequestMapping("/member")
public class memberAction {

    @Autowired
    private ManageService manageService;


    @GetMapping("getDeptNzTree")
    @ResponseBody
    public HashMap getDeptNzTree() throws Exception {
        HashMap m = new HashMap();

        try {
            List<NzTreeVo> result = new ArrayList<>();
            Dept root = manageService.getDept("ROOT");
            NzTreeVo rv = new NzTreeVo();
            rv.setTitle(root.getName());
            rv.setKey(root.getDeptid());
            rv.setExpanded(true);
            rv.setSelectable(true);
            List<NzTreeVo> child = new ArrayList<>();
            child = manageService.getDeptNzTreeByPid(root.getDeptid());
            rv.setChildren(child);
            result.add(rv);
            m.put("msg", "ok");
            m.put("list", result);
        } catch (Exception e) {
            e.printStackTrace();
            m.put("msg", "error");
        }


        return m;
    }


    @PostMapping("getMemberListByDeptId")
    @ResponseBody
    public HashMap getMemberListById(@RequestBody Member member) throws Exception {

        HashMap m = new HashMap();

        try {
            List<Member> ml = manageService.getMemberListByDept(member.getDept());
            if (ml.size() > 0) {


                for (Member t : ml) {
                    if (!Tools.isNullOrSpace(t.getDept())) {
                        Dept d = manageService.getDept(t.getDept());
                        String deptname = d == null ? "" : d.getName();
                        t.setDept(deptname);
                    }

                    if (!Tools.isNullOrSpace(t.getRolegroup())) {
                        Rolegroup r = manageService.getRoleGroup(t.getRolegroup());
                        String rolename = r == null ? "" : r.getName();
                        t.setRolegroup(rolename);
                    }


                }
                m.put("msg", "ok");
                m.put("list", ml);
            } else {
                m.put("msg", "empty");

            }
        } catch (Exception e) {
            e.printStackTrace();
            m.put("msg", "error");
        }

        return m;
    }

    @PostMapping("saveDept")
    @ResponseBody
    public HashMap saveDept(@RequestBody Dept dept) throws Exception {
        HashMap m = new HashMap();
        if (Tools.isNullOrSpace(dept.getDeptid())) {
            dept.setDeptid(Tools.newUUID());
        }
        Dept temp = manageService.saveDept(dept);
        if (temp != null) {
            m.put("msg", "ok");
            m.put("obj", temp);
        } else {
            m.put("msg", "error");
        }


        return m;
    }

    @PostMapping("getSingleDept")
    @ResponseBody
    public HashMap getSingleDept(@RequestBody Dept dept) throws Exception {

        HashMap m = new HashMap();

        Dept temp = manageService.getDept(dept.getDeptid());
        if (temp != null) {
            m.put("msg", "ok");
            m.put("obj", temp);
        } else {
            m.put("msg", "error");
        }

        return m;
    }


    @PostMapping("deleteDept")
    @ResponseBody
    public HashMap deleteDept(@RequestBody Dept dept) throws Exception {

        HashMap m = new HashMap();

        boolean flag = manageService.deleteDept(dept.getDeptid());
        if (flag) {
            m.put("msg", "ok");

        } else {
            m.put("msg", "error");
        }


        return m;
    }

    @PostMapping("getMemberById")
    @ResponseBody
    public HashMap getMemberById(@RequestBody Member member) throws Exception {
        HashMap m = new HashMap();

        try {
            Member temp = manageService.getMember(member.getMemberid());
            if (temp != null) {
                m.put("msg", "ok");
                m.put("obj", temp);
            } else {
                m.put("msg", "empty");
            }
        } catch (Exception e) {
            e.printStackTrace();
            m.put("msg", "error");
        }

        return m;
    }

    @PostMapping("saveMember")
    @ResponseBody
    public HashMap saveMember(@RequestBody Member member) throws Exception {
        HashMap m = new HashMap();
        if (Tools.isNullOrSpace(member.getMemberid())) {
            member.setMemberid(Tools.newUUID());
        }
        if(manageService.isExsistUserName(member.getName())){
            m.put("msg", "namerepeat");
        }else{
            Member temp = manageService.saveMember(member);
            if (temp != null) {
                m.put("msg", "ok");
                m.put("obj", temp);
            } else {
                m.put("msg", "error");
            }
        }
        return m;
    }
   @PostMapping("updateMember")
    @ResponseBody
    public HashMap updateMember(@RequestBody Member member) throws Exception {
        HashMap m = new HashMap();
        if (Tools.isNullOrSpace(member.getMemberid())) {
            member.setMemberid(Tools.newUUID());
        }

            Member temp = manageService.saveMember(member);
            if (temp != null) {
                m.put("msg", "ok");
                m.put("obj", temp);
            } else {
                m.put("msg", "error");
            }

        return m;
    }

    @GetMapping("getRoleGroupNzSelList")
    @ResponseBody
    public HashMap getRoleGroupNzSelList() throws Exception {
        HashMap m = new HashMap();
        try {
            List<Rolegroup> rl = manageService.getRoleGroupList();
            if (rl.size() > 0) {
                List<NzSelOption> result = new ArrayList<>();
                for (Rolegroup r : rl) {
                    NzSelOption o = new NzSelOption();
                    o.setLabel(r.getName());
                    o.setValue(r.getRolegroupid());
                    result.add(o);
                }

                m.put("msg", "ok");
                m.put("list", result);
            } else {
                m.put("msg", "empty");
            }
        } catch (Exception e) {
            e.printStackTrace();
            m.put("msg", "error");
        }


        return m;
    }

    @PostMapping("deleteMember")
    @ResponseBody
    public HashMap deleteMember(@RequestBody Member member) throws Exception {

        HashMap m = new HashMap();

        boolean flag = manageService.deleteMember(member.getMemberid());
        if (flag) {
            m.put("msg", "ok");
        } else {
            m.put("msg", "error");
        }


        return m;
    }


    @GetMapping("getRoleGroupList")
    @ResponseBody
    public HashMap getRoleGroupList() throws Exception {
        HashMap m = new HashMap();
        try {
            List<Rolegroup> rl = manageService.getRoleGroupList();
            if (rl.size() > 0) {
                m.put("msg", "ok");
                m.put("list", rl);
            } else {
                m.put("msg", "empty");
            }
        } catch (Exception e) {
            e.printStackTrace();
            m.put("msg", "error");
        }


        return m;
    }

    @PostMapping("deleteRoleGroup")
    @ResponseBody
    public HashMap deleteRoleGroup(@RequestBody Rolegroup rolegroup) throws Exception {

        HashMap m = new HashMap();
        try {
            List<Roleresource> rl = manageService.getRoleResourceByRole(rolegroup.getRolegroupid());
            if (rl.size() > 0) {
                manageService.deleteRoleResources(rl);
            }
            boolean flag = manageService.deleteRoleGroup(rolegroup.getRolegroupid());
            if (flag) {
                m.put("msg", "ok");

            } else {
                m.put("msg", "error");
            }
        } catch (Exception e) {
            e.printStackTrace();
            m.put("msg", "error");
        }


        return m;
    }

    @PostMapping("getRoleGroupById")
    @ResponseBody
    public HashMap getRoleGroupById(@RequestBody Rolegroup rolegroup) throws Exception {

        HashMap m = new HashMap();

        try {
            Rolegroup temp = manageService.getRoleGroup(rolegroup.getRolegroupid());
            if (temp != null) {
                m.put("msg", "ok");
                m.put("obj", temp);
            } else {
                m.put("msg", "empty");
            }
        } catch (Exception e) {
            e.printStackTrace();
            m.put("msg", "error");
        }

        return m;
    }

    @PostMapping("saveRoleGroup")
    @ResponseBody
    public HashMap saveRoleGroup(@RequestBody Rolegroup rolegroup) throws Exception {
        HashMap m = new HashMap();
        if (Tools.isNullOrSpace(rolegroup.getRolegroupid())) {
            rolegroup.setRolegroupid(Tools.newUUID());
        }
        Rolegroup temp = manageService.saveRoleGroup(rolegroup);
        if (temp != null) {

            m.put("msg", "ok");
            m.put("obj", temp);

        } else {
            m.put("msg", "error");
        }


        return m;

    }

    @PostMapping("getRightNzTreeByType")
    @ResponseBody
    public HashMap getRightNzTreeByType(@RequestBody Rightresource rightresource) throws Exception {
        HashMap m = new HashMap();

        try {
            List<NzTreeVo> result = new ArrayList<>();

            Rightresource root = null;
            if (rightresource.getType().equals("ACTION")) {
                root = manageService.getRightResource("ROOTACTION");
            } else {
                root = manageService.getRightResource("ROOTMENU");
            }
            NzTreeVo v = new NzTreeVo();
            v.setTitle(root.getName());
            v.setKey(root.getResourceid());
            v.setExpanded(false);
            List<NzTreeVo> child = new ArrayList<>();


            child.addAll(manageService.getRightResourceNztreeViByPid(root.getResourceid()));
            v.setChildren(child);
            result.add(v);


            m.put("msg", "ok");
            m.put("list", result);
        } catch (Exception e) {
            e.printStackTrace();
            m.put("msg", "error");
        }


        return m;
    }

    @PostMapping("getResourserTreeByTypeAndRole")
    @ResponseBody
    public HashMap getResourserTreeByTypeAndRole(@RequestBody Roleresource roleresource) {
        HashMap hashMap = new HashMap();
        try {
            List<Roleresource> roleResourceByRoleAndType = manageService.getRoleResourceByRoleAndType(roleresource.getRole(), roleresource.getType());
            List<String> result = new ArrayList<>();
            for (Roleresource roleresource1 : roleResourceByRoleAndType) {
                result.add(roleresource1.getResource());
            }
            hashMap.put("msg", "ok");
            hashMap.put("list", result);
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("saveRoleResourse")
    @ResponseBody
    public HashMap saveRoleResourse(@RequestBody RoleResourceParm roleResourceParm) {
        HashMap m = new HashMap();

        try {
            List<Roleresource> todeletelist = manageService.getRoleResourceByRoleAndType(roleResourceParm.getRole(), roleResourceParm.getType());
            if (todeletelist.size() > 0) {
                manageService.deleteRoleResources(todeletelist);
            }

            if (roleResourceParm.getList().size() > 0) {
                manageService.saveRoleResource(roleResourceParm.getList());
            }
            m.put("msg", "ok");
        } catch (Exception e) {
            e.printStackTrace();
            m.put("msg", "error");
        }


        return m;
    }

    @PostMapping("getRightresourceByid")
    @ResponseBody
    public HashMap getRightresourceByid(@RequestBody Rightresource roleresource) {
        HashMap hashMap = new HashMap();
        try {
            Rightresource roleResource = manageService.getRightResource(roleresource.getResourceid());
            hashMap.put("msg", "ok");
            hashMap.put("obj", roleResource);
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("saveRightresource")
    @ResponseBody
    public HashMap saveRightresource(@RequestBody Rightresource roleresource) {
        HashMap hashMap = new HashMap();
        try {
            if(Tools.isNullOrSpace(roleresource.getResourceid())){
                roleresource.setResourceid(Tools.newUUID());
            }
            manageService.saveRightResource(roleresource);
            hashMap.put("msg", "ok");

        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("deleteRightresource")
    @ResponseBody
    public HashMap deleteRightresource(@RequestBody Rightresource roleresource) {
        HashMap hashMap = new HashMap();
        try {
            manageService.deleteRightResource(roleresource.getResourceid());
            hashMap.put("msg", "ok");

        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }
        return hashMap;
    }

    @PostMapping("getRightNzTreeByTypeForZyzc")
    @ResponseBody
    public HashMap getRightNzTreeByTypeForZyzc(@RequestBody Rightresource rightresource) throws Exception {
        HashMap m = new HashMap();

        try {
            List<NzTreeVo> result = new ArrayList<>();

            Rightresource root = null;
            if (rightresource.getType().equals("ACTION")) {
                root = manageService.getRightResource("ROOTACTION");
            } else {
                root = manageService.getRightResource("ROOTMENU");
            }
            NzTreeVo v = new NzTreeVo();
            v.setTitle(root.getName());
            v.setKey(root.getResourceid());
            v.setExpanded(true);
            List<NzTreeVo> child = new ArrayList<>();
            child.addAll(manageService.getRightResourceNztreeViByPidForZyzc(root.getResourceid()));
            v.setChildren(child);
            result.add(v);


            m.put("msg", "ok");
            m.put("list", result);
        } catch (Exception e) {
            e.printStackTrace();
            m.put("msg", "error");
        }


        return m;
    }


    @GetMapping("getMenulist")
    @ResponseBody
    public HashMap getMenuList() throws Exception{
        HashMap m=new HashMap();
        try {
            String userid=manageService.getCurrentUserId();
            Member member=manageService.getMember(userid);
            if(member!=null){
                String role=member.getRolegroup();
                List<MenuTreeVo> result=new ArrayList<>();
                List<Rightresource> menulist=manageService.getRightRightResourceListByPid("ROOTMENU");
                for(Rightresource r:menulist){
                    MenuTreeVo v=new MenuTreeVo();
                    v.setName(r.getName());
                    v.setPath(r.getPath());
                    List<Rightresource> childlist=manageService.getRightRightResourceListByPid(r.getResourceid());
                    List<MenuTreeVo> vclist=new ArrayList<>();
                    for(Rightresource s:childlist){
                        MenuTreeVo v1=new MenuTreeVo();
                        v1.setName(s.getName());
                        v1.setPath(s.getPath());
                        if(manageService.haveRightByResourceIdAndRole(s.getResourceid(),role)){
                            vclist.add(v1);
                        }

                    }
                    v.setChild(vclist);

                    if(manageService.haveRightByResourceIdAndRole(r.getResourceid(),role)){
//                        根据权限范围决定菜单
                        result.add(v);
                    }


                }




                m.put("msg","ok");
                m.put("list",result);

            }
            else {
                m.put("msg","parmerror");
            }
        } catch (Exception e) {
            e.printStackTrace();
            m.put("msg","error");
        }


        return m;
    }


    @PostMapping("getMemberListByRole")
    @ResponseBody
    public  HashMap getMemberListByRole(@RequestBody PageParmVo pageParmVo){
        HashMap hashMap = new HashMap();
        try {
            List<NzSelOption> result = new ArrayList<>();
            List<Member> memberListByRole = manageService.getMemberListByDept(pageParmVo.getParm());
            for (Member member : memberListByRole) {
                NzSelOption nzSelOption = new NzSelOption();
                nzSelOption.setLabel(member.getRealname());
                nzSelOption.setValue(member.getMemberid());
                result.add(nzSelOption);
            }

            hashMap.put("list",result);
            hashMap.put("msg","ok");
        } catch (Exception e) {
            hashMap.put("msg","error");
            e.printStackTrace();
        }


        return hashMap;
    }
}
